package com.dean.scoket;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class DeanScoketSever {
    public static  ConcurrentMap<String,Socket> users = new ConcurrentHashMap<>();

    public static ConcurrentMap<String,Socket> getUsers() {
        if(users == null){
            synchronized (DeanScoketSever.class){
                if(users == null){
                    users = new ConcurrentHashMap<>();
                }
            }
        }
        return users;
    }

    public void start() throws IOException {
        ServerSocket server = new ServerSocket(8080);
        Socket client;
        while(true){
            // 接受请求
            client = server.accept();
            // 启用一个线程用于接受消息
            new Thread(new DeanScoketServerReceiveMsg(client)).start();
        }
    }

    public static void main(String[] args) throws IOException {
        DeanScoketSever sever = new DeanScoketSever();
        new Thread() {
            @Override
            public void run() {
                try {
                    sever.start();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }.start();
        // 启动客户端
        DeanScokteClient client = new DeanScokteClient();
        client.start("dean1");
    }


}
